package com.tanhua.dubbo.api;

import com.tanhua.domain.mongo.RecommendUser;
import com.tanhua.domain.vo.PageResult;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;


@Service
public class RecommendApiImpl implements RecommendUserApi {
    @Autowired
    private MongoTemplate mongoTemplate;
    /**
     * 根据toUserId查询推荐分数最高的好友
     */
    @Override
    public RecommendUser findMaxScoreUser(Long toUserId) {
        Query query = new Query();
        Criteria criteria = Criteria.where("toUserId").is(toUserId)
                /*.and("date").is(new DateTime().toString("yyyy/MM/dd"))*/;
        query.addCriteria(criteria);
        query.with(Sort.by(Sort.Order.desc("score")));
        query.limit(1);
        return mongoTemplate.findOne(query, RecommendUser.class);
    }

    //自定义 recommendUser
    @Override
    public void save(RecommendUser recommendUser) {
        mongoTemplate.insert(recommendUser);
    }

    /**
     * 查询推荐给当前用户的好友列表
     */
    @Override
    public PageResult findRecommendation(Integer page, Integer pagesize, Long toUserId) {
        Query query = new Query();
        query.addCriteria(Criteria.where("toUserId").is(toUserId)
                .and("date").is("2020/10/11"));
        query.with(PageRequest.of(page - 1, pagesize));
        query.with(Sort.by(Sort.Order.desc("score")));

        List<RecommendUser> list = mongoTemplate.find(query, RecommendUser.class);
        long count = mongoTemplate.count(query, RecommendUser.class);

        int pages = (int) Math.ceil(count * 1.0 / pagesize);
        return new PageResult((int) count, pagesize, pages, page, list);
    }

    @Override
    public Integer findRecommendScore(Long userId, Long targetId) {
        Query query = Query.query(Criteria.where("userId").is(targetId).and("toUserId").is(userId));
        query.limit(1);
        RecommendUser recommendUser = mongoTemplate.findOne(query, RecommendUser.class);
        if (recommendUser != null) {
            return recommendUser.getScore().intValue();
        }
        return null;
    }

    /**
     * 获取左右滑用的推荐集合
     *
     * @param userId
     * @return
     */
    @Override
    public List<Long> getRecommendUserCards(Long userId, Set<Long> utilSet) {
        Query query = new Query();
        Criteria criteria = Criteria.where("toUserId").is(userId).and("userId").nin(utilSet);
        query.addCriteria(criteria);
        query.with(Sort.by(Sort.Order.desc("score")));
        List<RecommendUser> recommendUsers = mongoTemplate.find(query, RecommendUser.class);
        if (recommendUsers == null || recommendUsers.size() == 0) {
            return null;
        }
        List<Long> recommendUserIdList = new ArrayList<>();
        for (RecommendUser recommendUser : recommendUsers) {
            recommendUserIdList.add(recommendUser.getUserId());
        }
        return recommendUserIdList;
    }
}
